Welcome back.
So as we talked about before, a while loop is generally more capable than a for
loop but there are few drawbacks to using while loops.
The first is that it can be a little bit more tedious to express the same concept,
as you might have seen in some of the code samples from before.
But the second is that you can get stuck in what's called an Infinite Loop.
An Infinite Loop is a loop that never terminates.
In other words, your program, if it could, would keep running forever.
Now remember that if we have a while loop like this,
then every while loop has a condition.
And by the time we get to the end of the while loop,
then we're going to go back to the top and check if this condition is still true.
And what that means is that if you ever run the code in this while loop,
then it better have a chance of switching this condition from false to true.
But if that's not the case, in other words, if this condition is always true
and we always reach the end of this while loop,
then our code is going to be stuck in what's called an Infinite Loop.
Because if we always reach the end of this while loop,
then we're always going to go back to the top.
And if this condition is always true,
then we're always going to run this code once more.
And that's called an Infinite Loop because your program would
keep running infinitely if it could.
In reality of this textbook has a mechanism to
prevent your code from running too long and
pretty much every interpreter has some way of breaking out of an Infinite Loop.
But they can be frustrating, nonetheless, and
they're important to be able to recognize.
Now this code is actually stuck in an Infinite Loop.
So if I ran my code, now I would see first that my cursor
is stuck on a pointer, so that tells me that is something is wrong.
If I even try to click or type anything
then I am not going to be able to, because this computer is working so
hard trying to run this little while look that has three lines.
Now, after a while then,
the Python interpreter is going to say that this code ran too long but,
until then, I'm not going to be able to do anything on the page.
So, as it's evaluating this,
let's try to analyze why we're actually stuck in an Infinite Loop.
So, here we go.
The code finally finished running and
you can see that it printed out Bugs many, many times.
So, I'm going to scroll to the bottom here.
You can see that it printed out Bugs a lot.
And at the end,
I'm going to get a message that this program exceeded the run time limit.
So in other words, this program tried to run for too long and
this probably indicates that your program is stuck in an Infinite Loop.
So let's look at why this program is stuck in an Infinite Loop.
So here we initialize the variable b to be 15.
So that looks good so far and we say while b is less than 60.
Okay, so this condition is of course going to be true while b is 15.
So when b is 15, then we're going to run this code.
And then we say b = 5.
Hm, well,
that looks a little bit out of place, but let's come back to it in a little bit.
On line 5, we print out Bugs,
which as you can see gets printed out a lot because this while loop is run a lot.
And then on line 6 we say b equals its previous value plus 7.
Now, let's come back to this line b = 5 because
what assigning b = 5 does here is it kind of resets the value of b.
So again our check is if b is less than 60.
And so if we keep resetting b to be 5 even if we increment its value
to be 12 later on by adding 7 to it then b is still always going to be less than
60 because every time we run this loop we reset b's value to 5.
So maybe if line 4 was accidental, so if we commented it out,
then we would actually be able to run our code.
So again,
the problem here is that we were resetting b to be 5 every time we ran this code.
So, it's important to be able to recognize how Infinite Loops occur.
They can occur in many, many ways.
So, this example is just one of many ways to accidentally write an Infinite Loop.
As you get more experience in writing while loops,
you'll become more adept at being able to identify and fix Infinite Loops.
That's all for now until next time.